package com.boot.security.server.service.impl;

import com.boot.security.server.common.Result;
import com.boot.security.server.common.ResultBuilder;
import com.boot.security.server.dao.QuestionRecordDao;
import com.boot.security.server.dto.LoginUser;
import com.boot.security.server.mapper.MOverseeQuestionMapper;
import com.boot.security.server.mapper.MQuestionRecordMapper;
import com.boot.security.server.mapper.SysRoleUserMapper;
import com.boot.security.server.model.QuestionRecord;
import com.boot.security.server.model.domain.MOverseeQuestion;
import com.boot.security.server.model.domain.MQuestionRecord;
import com.boot.security.server.model.domain.SysRoleUser;
import com.boot.security.server.model.param.ChangeStatusParam;
import com.boot.security.server.service.ReviewService;
import com.boot.security.server.utils.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 */
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private MOverseeQuestionMapper overseeQuestionMapper;

    @Autowired
    private QuestionRecordDao questionRecordDao;

    @Autowired
    private MQuestionRecordMapper questionRecordMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    /**
     *
     **/
    @Override
    public Result reviewQuestion (ChangeStatusParam param) {

        MOverseeQuestion mOverseeQuestion = overseeQuestionMapper.selectByPrimaryKey(param.getId());
        mOverseeQuestion.setCheckStatus(param.getStatus());
        mOverseeQuestion.setUpdateTime(new Date());
        int i = overseeQuestionMapper.updateByPrimaryKeySelective(mOverseeQuestion);

        if (i > 0) {
            // 修改成功写入问题记录表
            MQuestionRecord record = new MQuestionRecord();
            record.setOverseeForm(mOverseeQuestion.getOverseeForm());
            record.setBranch(mOverseeQuestion.getBranch());
            record.setCheckStatus(param.getStatus());
            record.setType(1);
            record.setOverseeQuestion(mOverseeQuestion.getQuestion());
            record.setQuestionStatus(mOverseeQuestion.getQuestionStatus());
            record.setOverseeQuestionId(Integer.valueOf(param.getId()));
            // 制空id 数据库插入新纪录
            record.setId(null);
            if (StringUtils.isNotBlank(param.getRemark())) {
                record.setDetail(param.getRemark());
            }
            record.setCreateTime(new Date());
            LoginUser loginUser = UserUtil.getLoginUser();
            if (loginUser != null) {
                record.setUpdateUser(loginUser.getUsername());
            }
            if (param.getFeedBackDate() != null) {
                record.setFeedBackDate(param.getFeedBackDate());
            }
            int i1 = questionRecordMapper.insertSelective(record);
            if (i1 < 0) {
                return ResultBuilder.systemError("插入记录失败，请联系管理员！");
            }
            return ResultBuilder.success();
        }
        return ResultBuilder.paramError("修改失败，你刷新重试!");
    }

    /**
     *
     **/
    @Override
    public Result reviewQuestionStatus (ChangeStatusParam param) {
        // 查询问题详情 修改状态
        MOverseeQuestion mOverseeQuestion = overseeQuestionMapper.selectByPrimaryKey(Integer.valueOf(param.getId()));
        if (mOverseeQuestion.getQuestionStatus().equals(param.getStatus())) {
            return ResultBuilder.success();
        }
        mOverseeQuestion.setUpdateTime(new Date());
        mOverseeQuestion.setQuestionStatus(param.getStatus());
        int i = overseeQuestionMapper.updateByPrimaryKeySelective(mOverseeQuestion);

        if (i > 0) {
            // 修改成功写入问题记录表
            MQuestionRecord record = new MQuestionRecord();
            record.setOverseeForm(mOverseeQuestion.getOverseeForm());
            record.setCheckStatus(mOverseeQuestion.getCheckStatus());
            record.setOverseeQuestionId(Integer.valueOf(param.getId()));
            record.setQuestionStatus(param.getStatus());
            record.setOverseeQuestion(mOverseeQuestion.getQuestion());
            record.setBranch(mOverseeQuestion.getBranch());
            // 制空id 数据库插入新纪录
            record.setId(null);
            if (StringUtils.isNotBlank(param.getRemark())) {
                record.setDetail(param.getRemark());
            }
            record.setCreateTime(new Date());
            LoginUser loginUser = UserUtil.getLoginUser();
            if (loginUser != null) {
                record.setUpdateUser(loginUser.getUsername());
            }
            int i1 = questionRecordMapper.insertSelective(record);
            return ResultBuilder.success();
        }
        return ResultBuilder.paramError("修改失败，请刷新重试!");
    }
}
